<template>
  <div>
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'"
      label-width="120px">
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="设备编码" prop="code" :rules="[
                 ]">
            <el-input v-model="inputForm.code" placeholder="请填写设备编码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="厂" prop="factory" :rules="[
                 ]">
            <el-input v-model="inputForm.factory" placeholder="请填写厂"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="异常区域" prop="abnormalArea" :rules="[
                 ]">
            <el-input v-model="inputForm.abnormalArea" placeholder="请填写异常区域"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="异常设备" prop="abnormalObject" :rules="[
                 ]">
            <el-input v-model="inputForm.abnormalObject" placeholder="请填写异常设备"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="异常信息" prop="abnormalMessgae" :rules="[
                 ]">
            <el-input v-model="inputForm.abnormalMessgae" placeholder="请填写异常信息"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="异常等级" prop="abnormalLevel" :rules="[
                 ]">
            <el-select v-model="inputForm.abnormalLevel" placeholder="请选择" style="width: 100%; color: yellow;" >
              <el-option v-for="item in $dictUtils.getDictList('abnormal_level')" :key="item.value" :label="item.label"
                :value="item.value" style="color: ${this.uuu};">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="第一责任人" prop="firstUser.id" :rules="[
                 ]">
            <user-select :limit='1' :value="inputForm.firstUser.id" @getValue='(value) => {inputForm.firstUser.id=value}' @getUserName='getUserName(arguments)'></user-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工号" prop="userId" :rules="[
                 ]">
            <el-input v-model="inputForm.userId" placeholder="请填写工号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划检修工时" prop="overhaulPeople" :rules="[
                 ]">
            <el-input v-model="inputForm.overhaulPeople" placeholder="请填写工时"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划检修人数" prop="repirpeople" :rules="[
                 ]">
            <el-input v-model="inputForm.repirpeople" placeholder="请填写人数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="完成时间" prop="finishTime" :rules="[
                 ]">
            <el-date-picker v-model="inputForm.finishTime" type="datetime" style="width: 100%;" value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检修性质" prop="remarks" :rules="[
                 ]">
            <el-select v-model="inputForm.remarks" placeholder="请选择" style="width: 100%;">
              <el-option v-for="item in $dictUtils.getDictList('repair_nature')" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="异常处置方案" prop="abnormaltry" :rules="[
                 ]">
            <el-input type="textarea" v-model="inputForm.abnormaltry" placeholder="请填写异常处置方案"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="0">
            <el-tabs v-model="abnormalProcessTab">
              <el-tab-pane label="异常信息">
                <!-- <el-button size="small" @click="addAbnormalProcessChildRow" type="primary">新增</el-button> -->
                <el-table border :data="inputForm.abnormalProcessChildList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                  <el-table-column prop="name" header-align="center" align="center" show-overflow-tooltip label="指标名称">
                  </el-table-column>
                  <el-table-column prop="checkCategory" header-align="center" align="center" show-overflow-tooltip
                    label="类别">
                    <template slot-scope="scope">
                      {{ $dictUtils.getDictLabel("repair_opinion", scope.row.checkCategory, '-') }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="checkUnit" header-align="center" align="center" show-overflow-tooltip label="计量单位">
                    <template slot-scope="scope">
                      {{ $dictUtils.getDictLabel("basicdata_symbol_measure", scope.row.checkUnit, '-') }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="checkJudgeSymbol" header-align="center" align="center" show-overflow-tooltip
                    label="上限判断符号">
                    <template slot-scope="scope">
                      {{ $dictUtils.getDictLabel("basicdata_symbol", scope.row.checkJudgeSymbol, '-') }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="checkDownJudgeSymbol" header-align="center" align="center"
                    show-overflow-tooltip label="下限判断符号">
                  </el-table-column>
                  <el-table-column prop="checkUp" header-align="center" align="center" show-overflow-tooltip label="上限">
                  </el-table-column>
                  <el-table-column prop="checkLimit" header-align="center" align="center" show-overflow-tooltip label="上极限">
                  </el-table-column>
                  <el-table-column prop="checkDown" header-align="center" align="center" show-overflow-tooltip label="下限">
                  </el-table-column>
                  <el-table-column prop="checkDownLimit" header-align="center" align="center" show-overflow-tooltip
                    label="下极限">
                  </el-table-column>
                  <el-table-column prop="isnormal" header-align="center" align="center" show-overflow-tooltip label="是否异常">
                    <template slot-scope="scope">
                      {{ $dictUtils.getDictLabel("true_false", scope.row.isnormal, '-') }}
                    </template>
                  </el-table-column>
                  <el-table-column
                     prop="normalLevel"
                     header-align="center"
                     align="center"
                     show-overflow-tooltip
                     label="异常等级">
                     <template slot-scope="scope">
                         <el-tag type="warning" effect="dark" v-if="scope.row.normalLevel === '1'">
                           {{ $dictUtils.getDictLabel('abnormal_level', scope.row.normalLevel, '-') }}
                         </el-tag>
                         <el-tag type="danger" effect="dark" v-if="scope.row.normalLevel === '3'">
                           {{ $dictUtils.getDictLabel('abnormal_level', scope.row.normalLevel, '-') }}
                         </el-tag>
                     </template>
                   </el-table-column>
                  <el-table-column prop="checkJudge" header-align="center" align="center" show-overflow-tooltip label="是否手动">
                    <template slot-scope="scope">
                      {{ $dictUtils.getDictLabel("true_false", scope.row.checkJudge, '-') }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="checkContent" header-align="center" align="center" show-overflow-tooltip label="点检标准">
                  </el-table-column>
                  <el-table-column prop="input" header-align="center" align="center" show-overflow-tooltip label="输入">
                  </el-table-column>
                  <el-table-column prop="remarks" header-align="center" align="center" show-overflow-tooltip label="备注信息">
                  </el-table-column>
                  <el-table-column fixed="right" label="操作" width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewAbnormalProcessChildRow(scope.row)" type="text" size="small">查看</el-button>
                      <el-button @click="editAbnormalProcessChildRow(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button @click="delAbnormalProcessChildRow(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <AbnormalProcessChildForm ref="abnormalProcessChildForm" @addRow="saveAbnormalProcessChildRow(arguments)"></AbnormalProcessChildForm>
  </div>
</template>

<script>
  import AbnormalProcessChildForm from './AbnormalProcessChildForm'
  import UserSelect from '@/components/userSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        abnormalProcessTab: '0',
        uuu: 'yellow',
        inputForm: {
          id: '',
          repirpeople: '',
          abnormalProcessChildList: this.inits.abnormalProcessChildList,
          code: this.inits.code,
          factory: this.inits.factory,
          abnormalArea: this.inits.warningArea,
          abnormalObject: this.inits.warningObject,
          abnormalMessgae: '',
          abnormalLevel: '',
          firstUser: {
            id: ''
          },
          userId: '',
          overhaulPeople: '',
          finishTime: '',
          remarks: '',
          abnormaltry: ''
        }
      }
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      inits: {
        code: '',
        factory: '',
        warningArea: '',
        warningObject: '',
        abnormalProcessChildList: []
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    components: {
      UserSelect,
      AbnormalProcessChildForm
    },
    watch: {
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            this.init(this.businessId)
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields()
            })
          }
        },
        immediate: true,
        deep: false
      }
    },
    methods: {
       // 获取工号值
      getUserName (name) {
        this.inputForm.userId = name[1]
      },
      init (id) {
        if (id) {
          this.loading = true
          this.inputForm.id = id
          this.$nextTick(() => {
            // this.$refs.inputForm.resetFields()
            this.$http({
              url: `/check/abnormalprocess/abnormalProcess/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.abnormalProcess)
              this.loading = false
            })
          })
        }
      },
      saveAbnormalProcessChildRow (child) {
        if (child[0] === '') {
          this.inputForm.abnormalProcessChildList.push(child[1])
        } else {
          this.inputForm.abnormalProcessChildList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.abnormalProcessChildList.splice(index, 1, child[1])
            }
          })
        }
      },
      addAbnormalProcessChildRow (child) {
        this.$refs.abnormalProcessChildForm.init('add')
      },
      viewAbnormalProcessChildRow (child) {
        this.$refs.abnormalProcessChildForm.init('view', child)
      },
      editAbnormalProcessChildRow (child) {
        this.$refs.abnormalProcessChildForm.init('edit', child)
      },
      delAbnormalProcessChildRow (child) {
        this.inputForm.abnormalProcessChildList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.abnormalProcessChildList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.abnormalProcessChildList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      saveForm (callback) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/check/abnormalprocess/abnormalProcess/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                callback(data.businessTable, data.businessId)
              }
            })
          }
        })
      }
      }
    }
</script>
